/* -------------------------------------------------------- antd -- */
import { useEffect } from 'react'
import { message } from 'antd'
/* -------------------------------------------------------- 全局配置 -- */
message.config({ top: '40%', duration:1 })
/* -------------------------------------------------------- 销毁 -- */
window.addEventListener('mousedown', e =>{
	const { className } = e.target
	if( typeof className === 'string' && !className.includes('ant-message-notice-content')){
		message?.destroy()
	}
})

let clear 

const timeout =  f => {
	clearTimeout(clear)
	clear = setTimeout(f)
}
/* -------------------------------------------------------- 确认弹窗 -- */
const Index = () => {
	const [ api, contextHolder ] = message.useMessage()
	
	useEffect(()=>{
		window.$fn.msg = {
			success(msg, delay){
				timeout(()=>api.success(msg, delay))
			},
			error(msg, delay= 3){
				timeout(()=>api.error(msg, delay))
			},
			info(msg, delay){
				timeout(()=>api.info(msg, delay))
			},
			warning(msg, delay){
				timeout(()=>api.warning(msg, delay))
			},
			warn(msg, delay){
				timeout(()=>api.warn(msg, delay))
			},
			loading(msg, delay){
				timeout(()=>api.loading(msg, delay))
			}
		}
	}, [ api ])
	
	return <>{contextHolder}</>
}
export default Index